package com.ny.zmb.glxt.controller;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ny.zmb.glxt.entity.PmProduct;
import com.ny.zmb.glxt.entity.PmStore;
import com.ny.zmb.glxt.service.IPmProductService;
import com.ny.zmb.openapi.controller.entity.Result;
import com.ny.zmb.openapi.ysutil.from.merchant.MerchantWithdraw;
import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@AllArgsConstructor
@RequestMapping("/glxt/pmProduct")
public class PmProductController {
    @Resource
    private IPmProductService pmProductService;

    @GetMapping("/page")
    public Result getPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String  orders,
            PmProduct pmProduct) {
        Page<PmProduct> page = new Page<>(current, size);
        if (orders != null && !orders.isEmpty()) {
            List<OrderItem> list = JSONObject.parseArray(orders, OrderItem.class);
            page.setOrders(list);
        }
        QueryWrapper<PmProduct> queryWrapper = new QueryWrapper<>(pmProduct);
        IPage<PmProduct> iPage = pmProductService.page(page, queryWrapper);
        return Result.ok(iPage);
    }

    @GetMapping("/getByIds")
    public Result getByIds(
            @RequestParam String ids) {
        List<Integer> idList = Arrays.stream(ids.split(","))
                .map(Integer::parseInt) // 将每个字符串元素转换为 Integer
                .collect(Collectors.toList()); // 收集到 List 中
        List<PmProduct> productList = pmProductService.listByIds(idList);
        return Result.ok(productList);
    }
}
